<template>
  <div class="comparison">
    <div class="table">
      <el-table
        v-loading="tableLoading"
        border
        :header-cell-style="headerClass"
        :span-method="objectMethod"
        style="width: 100%"
        :data="tableData"

      >
        <el-table-column label="月份" prop="type" align="center">
            <el-table-column label="月份" prop="class1" align="center"></el-table-column>
            <el-table-column label="月份" prop="class2" align="center"></el-table-column>
        </el-table-column>
        <el-table-column v-for="(item,index) in headerData" :key="index" :label="item" :prop="'s' + item" align="center">
            <template slot-scope="scope">
                <span>{{scope.row['s'+item]}}<span v-if="scope.row.class2 == '环比增幅'">%</span></span>
            </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { overall } from "@/api/barCode/index";
export default {
  name: "comparison",
  components: {},
  props: {
    form: {
      type: Object,
      default: {},
    },
  },
  watch: {
    form: {
      handler() {
        this.getData();
      },
      deep: true,
    },
  },
  data() {
    return {
      tableLoading: false,
      tableData: [],
      headerData: [],
      spanArr1: [],
    };
  },
  computed: {},
  created() {},
  mounted() {},
  methods: {
    getData() {
      this.tableLoading = true;
      overall(this.form).then((res) => {
        if (res.code == 200) {
          this.tableLoading = false;
          this.headerData = res.data.acctMonthS
          this.tableData = res.data.data;
          let contactDot = 0;
          let spanArr1 = [];
          this.tableData.forEach((item, index) => {
            if (index === 0) {
              spanArr1.push(1);
            } else {
              if (item.class1 === this.tableData[index - 1].class1) {
                spanArr1[contactDot] += 1;
                spanArr1.push(0);
              } else {
                contactDot = index;
                spanArr1.push(1);
              }
            }
          });
          this.spanArr1 = spanArr1;
        }
      });
    },
    objectMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (this.spanArr1[rowIndex]) {
          return {
            rowspan: this.spanArr1[rowIndex],
            colspan: 1,
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }
    },
    headerClass({rowIndex}) {
        if (rowIndex === 1) {
            return {display: 'none'}
        }
        return 'background:#0070C0;color:#FFFF'
    }
  },
};
</script>

<style scoped lang="scss">
</style>
